package com.sure.supermarketserver.sell.cash.controller;

<<<<<<< HEAD
import com.sure.supermarketserver.common.CommonService;
import com.sure.supermarketserver.common.Result;
import com.sure.supermarketserver.common.TokenUtils;
=======
import com.sure.supermarketserver.common.Result;
>>>>>>> 5f10f6198a25dcc9a1d966785717ca9d10c0d8ba
import com.sure.supermarketserver.sell.cash.model.to.CashCalcTo;
import com.sure.supermarketserver.sell.cash.model.to.CashEndPrice;
import com.sure.supermarketserver.sell.cash.model.to.CashOrderClient;
import com.sure.supermarketserver.sell.cash.model.vo.CashCalcVo;
import com.sure.supermarketserver.sell.cash.service.CashService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author Lei
 *
 * 控制层，访问的接口
 * @create 2021-07-21 13:07
 */
@RestController
@RequestMapping("/cash")
public class CashApi {

    @Resource
    private CashService cashService;

<<<<<<< HEAD

    @Resource
    private CommonService commonService;

=======
>>>>>>> 5f10f6198a25dcc9a1d966785717ca9d10c0d8ba
    /**
     * @Author Lei
     * @Description 每次输入go_id来进行查询返回一条记录的接口
     * @Date 20:08 2021/7/21
     * @Param [param]
     * 需要一个提前准备好的单号or_id，然后一个就是请求的go_id
     * @return java.lang.Object
     **/
    @PostMapping("/calc")
<<<<<<< HEAD
    private Object getCashCalc(@RequestBody CashCalcTo param,@RequestHeader("Token") String token) {
        String st_id = TokenUtils.getUserInfo(token,commonService).getUserId();
        Object o = cashService.calcByGoId(param.getOr_id(), param.getGo_id(),st_id);
=======
    private Object getCashCalc(@RequestBody CashCalcTo param) {
        Object o = cashService.calcByGoId(param.getOr_id(), param.getGo_id());
>>>>>>> 5f10f6198a25dcc9a1d966785717ca9d10c0d8ba
        if (o == null) {
            return Result.fail(300, "查不到这条货物的消息");
        } else {
            ArrayList<Object> cashCalcVos = new ArrayList<>();
            cashCalcVos.add(o);
            return Result.success(cashCalcVos);

        }
    }


    /**
     * @Author Lei
     * @Description 最开始进行调用的，获取or_id的一个接口
     * @Date 20:13 2021/7/21
     * @Param []
     * @return java.lang.Object
     **/
    @GetMapping("/getOrId")
    private Object getOrId() {
        String orId = cashService.getOrId();
        return Result.success("操作成功", orId);
    }

    /**
     * @Author Lei
     * @Description 进行结算，将最后的费用填到Order_information这张表里面
     * @Date 20:13 2021/7/21
     * @Param [cashEndPrice]
     * @return java.lang.Object
     **/
    @PostMapping("/endCalc")
<<<<<<< HEAD
    private Object endCalc(@RequestBody CashEndPrice cashEndPrice,@RequestHeader("Token") String token) {
        cashEndPrice.setSt_id(TokenUtils.getUserInfo(token,commonService).getUserId());
=======
    private Object endCalc(@RequestBody CashEndPrice cashEndPrice) {
>>>>>>> 5f10f6198a25dcc9a1d966785717ca9d10c0d8ba
        int res = cashService.updateLastPrice(cashEndPrice);
        if (res != 0) {
            return Result.success("结算成功");
        } else {
            return Result.fail(Result.ERR_CODE_SYS, "结算错误");
        }
    }


    /**
     * @Author Lei
     * @Description 添加积分的接口，将client id进行设置
     * @Date 20:14 2021/7/21
     * @Param [cashOrderClient]
     * @return java.lang.Object
     **/
    @PostMapping("/addScore")
    private Object addScore(@RequestBody CashOrderClient cashOrderClient){
        int res = cashService.addScore(cashOrderClient);
        if (res != 0) {
            return Result.success("更新积分成功");
        } else {
            return Result.fail(Result.ERR_CODE_SYS, "更新积分失败");
        }

    }




}
